package com.aeon.thumbnail.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

public class Thumb extends Composite {

	private Button mImageLabel;
	private Label mTextLabel;

	private Image mImage;
	private String mText = new String();

	
	boolean isSelected=false;
	public Thumb(Composite parent, int style) {
		super(parent, style);
		init();

	}

	private void init() {
		RowLayout layout = new RowLayout(SWT.VERTICAL);
		layout.center = true;
		setLayout(layout);

		mImageLabel = new Button(this, SWT.FLAT);

		mTextLabel = new Label(this, SWT.NONE);
		mTextLabel.setText(mText);

		addListeners(mImageLabel);
		
	}

	private void addListeners(Button mImageLabel2) {
		mImageLabel2.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				isSelected=false;
				if(!isSelected){
					mTextLabel.setBackground(mTextLabel.getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
					mTextLabel.setForeground(mTextLabel.getDisplay().getSystemColor(SWT.COLOR_LIST_FOREGROUND));
				}
			}
			
			@Override
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		mImageLabel2.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				isSelected=true;
				if(isSelected){
					mTextLabel.setBackground(mTextLabel.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION));
					mTextLabel.setForeground(mTextLabel.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
				}
				
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
	}

	public void setToolTipText(String toolTip) {
		mImageLabel.setToolTipText(toolTip);
		mTextLabel.setToolTipText(toolTip);
	}

	/**
	 * @return the mText
	 */
	public String getText() {
		return mText;
	}

	/**
	 * @param mText
	 *            the mText to set
	 */
	public void setText(String mText) {
		this.mText = mText;
		mTextLabel.setText(mText);
	}

	/**
	 * @return the mImage
	 */
	public Image getImage() {
		return mImage;
	}

	/**
	 * @param mImage
	 *            the mImage to set
	 */
	public void setImage(Image image) {
		this.mImage = image;
		mImageLabel.setImage(image);

	}

	public void setAlignment(int alignment) {
		mTextLabel.setAlignment(alignment);
	}

	@Override
	public Rectangle getBounds() {
		// TODO Auto-generated method stub
		return super.getBounds();
	}

	@Override
	public void setSize(int width, int height) {
		// TODO Auto-generated method stub
		super.setSize(width, height);
		mImageLabel.setSize((int)(width*.9),(int)(.6* height));
		
		
	}
	
}
